using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
using Framework.Manager;

namespace Framework.UI
{
    public class ControllerBase<V> : MonoBehaviour, IControllerBase
        where V : IViewBase
    {
        protected V mViewBase;

        public void Init()
        {
            mViewBase = GetComponent<V>();
            OnInit();
        }

        protected virtual void OnInit() { }

        #region Register
        protected void RegisterButton(string pGameObjectName, Action pCallback, ExButton.EnumCallbackType pCallbackType = ExButton.EnumCallbackType.Click)
        {
            UIButton uiButton = mViewBase.GetComponent<UIButton>(pGameObjectName);
            if (uiButton != null)
            {
                switch (pCallbackType)
                {
                    case ExButton.EnumCallbackType.Click:
                        uiButton.OnClick = pCallback;
                        break;
                    case ExButton.EnumCallbackType.DoubleClick:
                        uiButton.OnDoubleClick = pCallback;
                        break;
                    case ExButton.EnumCallbackType.PressBegin:
                        uiButton.OnPressBegin = pCallback;
                        break;
                    case ExButton.EnumCallbackType.Press:
                        uiButton.OnPress = pCallback;
                        break;
                    case ExButton.EnumCallbackType.PressEnd:
                        uiButton.OnPressEnd = pCallback;
                        break;
                }
            }
        }

        protected void RegisterSlider(string pGameObjectName, Action<float> pCallback, ExSlider.EnumCallbackType pCallbackType = ExSlider.EnumCallbackType.ValueChange)
        {
            UISlider uiSlider = mViewBase.GetComponent<UISlider>(pGameObjectName);
            if (uiSlider != null)
            {
                switch (pCallbackType)
                {
                    case ExSlider.EnumCallbackType.ValueChangeBegin:
                        uiSlider.OnValueChangeBegin = pCallback;
                        break;
                    case ExSlider.EnumCallbackType.ValueChange:
                        uiSlider.OnValueChange = pCallback;
                        break;
                    case ExSlider.EnumCallbackType.ValueChangeEnd:
                        uiSlider.OnValueChangeEnd = pCallback;
                        break;
                }
            }
        }

        protected void RegisterToggle(string pGameObjectName, Action<bool> pCallback, ExToggle.EnumCallbackType pCallbackType = ExToggle.EnumCallbackType.ValueChange)
        {
            UIToggle uiToggle = mViewBase.GetComponent<UIToggle>(pGameObjectName);
            if (uiToggle != null)
            {
                switch (pCallbackType)
                {
                    case ExToggle.EnumCallbackType.ValueChange:
                        uiToggle.OnValueChange = pCallback;
                        break;
                }
            }
        }
        #endregion
    }
}